<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="loginDom">
        <div>
            <span>用户名</span> <input type="text" name="user" value="ibas" id="inpUser"/>
        </div>
        <div>
            <span>密码</span> <input type="text" name="password" value="123456" id="inpPwd"/>
        </div>
        <div>
            <button onclick="login()">登录</button>
        </div>
    </div>
    <div id="auth" style="display: none;">
        <button onclick="toAuth()">去授权</button>
    </div>
    <div id="authDom" style="display: none;">
        <div>
            <span>id</span> <input disabled type="text" value="" id="authId"/>
        </div>
        <div>
            <span>问题</span> <input disabled type="text" value="" id="authQ"/>
        </div>
        <div>
            <span>答案</span> <input type="text" value="" id="authA"/>
        </div>
        <div>
            <button onclick="overAuth()">提交</button>
        </div>
    </div>
    <div id="authSuccess" style="display: none;">授权成功</div>
</body>
<script>
    window.login = function () {
        let user = document.getElementById('inpUser').value;
        let pass = document.getElementById('inpPwd').value;
        Requests.login(user,pass)
        .then(token => {
            Requests.setToken(token);
            document.getElementById('loginDom').style.display = 'none';
            document.getElementById('auth').style.display = 'block';
        }).catch(msg => {
            alert(msg);
        })
    }
    window.toAuth = function () {
        document.getElementById('auth').style.display = 'none';
        document.getElementById('authDom').style.display = 'block';
        // let q = document.getElementById('authQ').value;
        // let a = document.getElementById('authA').value;
        Requests.getToken()
        .then(token => {
            Requests.getAdminQuestion(token)
            .then(q => {
                document.getElementById('authId').value = q.id;
                document.getElementById('authQ').value = q.question;
            });
        })
    }
    window.overAuth = function () {
        let id = document.getElementById('authId').value;
        let a = document.getElementById('authA').value;
        let ep = location.search.toLowerCase().includes('editpages');
        Requests.getToken().then(token => {
            Requests.checkAdminQuestion(id,a,token)
            .then(d => {
                document.getElementById('authDom').style.display = 'none';
                document.getElementById('authSuccess').style.display = 'block';
                if (ep) {
                    location.href = './../EditPages/index.html?editpages';
                }
            }).catch(e => alert(e.message));
        })
    }
    window.main = function () {
    }

    let sc = document.createElement('script');
    sc.src = "./../../base/utils/loadFiles.js";
    sc.onload = function () {
        let files = [
            { link: './../../base/utils/requests.js',text: '' },
            { link: './../../base/loading/loading.css',text: '' },
            { link: './../../base/loading/Loading.js',text: 'Loading JS' },
        ];
        window.loading = null;
        if (window.parent !== window) {
            if (window.parent.loading) {
                window.loading = window.parent.loading;
                files.shift();
                files.shift();
            }
        }
        let lf = new LoadFiles();
        lf.addFile(files).load(o => {
            if (!o.start) {
                o.next();
                if (o.over) {
                    window.loading.hide();
                    window.main();
                } else if (o.file.text === 'Loading JS') {
                    window.loading = new Loading();
                }
            }
        });
    }
    document.head.append(sc);
</script>
</html>
